<template>
    <div class="sort">
    <el-row class="head">
        <el-col >——故障类别列表/故障类别</el-col>
    </el-row>
        <el-row class="title">
            <el-col>故障类别列表</el-col>
        </el-row>
        <el-form  :model="formdata" label-width="100px" class="formrule" :rules="rules" ref="ruleform">
            <el-form-item label="维修分类" prop="typeId">
                <el-select v-model="formdata.typeId" placeholder="请选择" >
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="故障类别" prop="description">
                <el-input type="textarea" v-model="formdata.description"  :rows="5" style=" width: 600px;"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" @click="onSubmit(formdata)" width="600">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "sort",
        data() {
            return {
                formdata:{
                    typeId: '',
                    description:''
                },
                options:[],
                rules:{
                    typeId: [
                        { required: true, message: '请选择维修分类', trigger: 'change' }
                    ],
                    description: [
                        { required: true, message: '请输入故障类别', trigger: 'blur' },
                        { min: 4, max: 127, message: '至少4个字符', trigger: 'blur' }
                    ],
                }

            }
        },
        created(){
        this.getTypes();
        },
        methods:{
            onSubmit(formName){
                this.$refs.ruleform.validate((valid) => {
                    if (valid) {
                        console.log(formName);
                        this.$axios.post("/api/v1/admin/description/insertuser",formName).then(result => {

                            return result.data;
                        })
                            .then(data1 => {
                                console.log(data1.data);
                                this.formdata.description='';
                                this.formdata.typeId='';
                            });
                        this.$message({
                            type: 'success',
                            message: '添加成功'
                        });
                    } else {
                        return false;
                    }
                });

            },
            getTypes()
            {
                this.$axios.post("/api/v1/user/type/list").then(result => {

                    return result.data;
                })
                    .then(data1 => {
                        this.options=data1.data;
                    });
            },
        }
    }
</script>

<style scoped>
    .el-input{
        width: 600px;
    }
</style>